﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using CMP.ERP.Model;
using CMP.ERP.BLL;

namespace CMP.ERP.Admin.Controllers
{
    public class Express_mateController : Controller
    {
        //
        // GET: /Express_mate/
        MateCompanyBLL bll = new MateCompanyBLL();
        ExpressCompanyBLL bll1 = new ExpressCompanyBLL();
        public ActionResult Exress_mate_Show()
        {
            //下拉框
            List<ExpressCompany> ex = new List<ExpressCompany>();
            DataTable da = bll1.ExpressCompanyShow();
            foreach (DataRow item in da.Rows)
            {
                ExpressCompany e = new ExpressCompany();
                e.Express_Company_Id = Convert.ToInt32(item[0]);
                e.Express_Company_Name = item[1].ToString();
                e.Express_Company_Charges = item[2].ToString();
                e.Express_Company_Address = item[3].ToString();
                e.Express_Company_Phone = item[4].ToString();
                ex.Add(e);
            }
            ViewBag.Express_Company_Id = new SelectList(ex, "Express_Company_Id", "Express_Company_Name");
            return View();
        }
        //根据快递公司编码查询匹配地址(省/市)
        public ActionResult ShowMatch(int id)
        {
            List<ExpressMatch> list = new List<ExpressMatch>();
            DataTable data = bll.ShowExpressMatch(id);
            foreach (DataRow item in data.Rows)
            {
                ExpressMatch e = new ExpressMatch();
                e.ExpressMatchId = Convert.ToInt32(item[0]);
                e.Express_Company_Id = Convert.ToInt32(item[1]);
                e.Express_Company_Name = item[2].ToString();
                e.P_Id = Convert.ToInt32(item[3]);
                e.P_Name = item[4].ToString();
                e.P_City = item[5].ToString();
                list.Add(e);
            }
            return Json(list);
        }
        //根据快递公司编码查询匹配地址(市/区)
        public ActionResult GetExpressMatchShow(int id)
        {
            List<ExpressMatch> list = new List<ExpressMatch>();
            DataTable data = bll.GetExpressMatchShow(id);
            foreach (DataRow item in data.Rows)
            {
                ExpressMatch e = new ExpressMatch();
                e.ExpressMatchId = Convert.ToInt32(item[0]);
                e.Express_Company_Id = Convert.ToInt32(item[1]);
                e.Express_Company_Name = item[2].ToString();
                e.P_Id = Convert.ToInt32(item[3]);
                e.P_Name = item[4].ToString();
                e.P_City = item[5].ToString();
                list.Add(e);
            }
            return Json(list);
        }
        //显示快递公司信息
        public ActionResult ShowExpressCompany()
        {
            List<ExpressCompany> ex = new List<ExpressCompany>();
            DataTable da = bll1.ExpressCompanyShow();
            foreach (DataRow item in da.Rows)
            {
                ExpressCompany e = new ExpressCompany();
                e.Express_Company_Id = Convert.ToInt32(item[0]);
                e.Express_Company_Name = item[1].ToString();
                e.Express_Company_Charges = item[2].ToString();
                e.Express_Company_Address = item[3].ToString();
                e.Express_Company_Phone = item[4].ToString();
                ex.Add(e);
            }
            return Json(ex);
        }
        //显示匹配地区范围
        public ActionResult ShowProvince()
        {
            List<Province> list = new List<Province>();
            DataTable data = bll.ShowProvince();
            foreach (DataRow item in data.Rows)
            {
                Province e = new Province();
                e.P_Id = Convert.ToInt32(item["P_Id"]);
                e.P_Name = item["P_Name"].ToString();
                e.P_City = item["P_City"].ToString();
                list.Add(e);
            }
            return Json(list);
        }
        //判断是否有上级城市
        public ActionResult ShowProvince1(string P_City)
        {
            List<Province> list = new List<Province>();
            DataTable data = bll.ShowProvince1(P_City);
            foreach (DataRow item in data.Rows)
            {
                Province e = new Province();
                e.P_Id = Convert.ToInt32(item["P_Id"]);
                e.P_Name = item["P_Name"].ToString();
                e.P_City = item["P_City"].ToString();
                list.Add(e);
            }
            return Json(list);
        }
        //根据快递公司编码查询匹配地址(所有)
        public ActionResult GetExpressMatch1(int id)
        {
            List<ExpressMatch> list = new List<ExpressMatch>();
            DataTable data = bll.GetExpressMatch1(id);
            foreach (DataRow item in data.Rows)
            {
                ExpressMatch e = new ExpressMatch();
                e.ExpressMatchId = Convert.ToInt32(item["ExpressMatchId"]);
                e.Express_Company_Id = Convert.ToInt32(item["Express_Company_Id"]);
                e.P_Id = Convert.ToInt32(item["P_Id"]);
                e.P_Name = item["P_Name"].ToString();
                list.Add(e);
            }
            return Json(list);
        }
        //根据快递公司名称查询快递匹配信息
        public ActionResult SelMateCompany(int? Express_Company_Id)
        {
            //下拉框
            List<ExpressCompany> ex = new List<ExpressCompany>();
            DataTable da = bll1.ExpressCompanyShow();
            foreach (DataRow item in da.Rows)
            {
                ExpressCompany e = new ExpressCompany();
                e.Express_Company_Id = Convert.ToInt32(item[0]);
                e.Express_Company_Name = item[1].ToString();
                e.Express_Company_Charges = item[2].ToString();
                e.Express_Company_Address = item[3].ToString();
                e.Express_Company_Phone = item[4].ToString();
                ex.Add(e);
            }
            ViewBag.Express_Company_Id = new SelectList(ex, "Express_Company_Id", "Express_Company_Name");

            List<ExpressMatch> list = new List<ExpressMatch>();
            DataTable data = bll.SelExpressMatch(Express_Company_Id);
            foreach (DataRow item in data.Rows)
            {
                ExpressMatch e = new ExpressMatch();
                e.ExpressMatchId = Convert.ToInt32(item[0]);
                e.Express_Company_Id = Convert.ToInt32(item[1]);
                e.Express_Company_Name = item[2].ToString();
                e.P_Id = Convert.ToInt32(item[3]);
                e.P_Name = item[4].ToString();
                e.P_City = item[5].ToString();
                list.Add(e);
            }
            return View("Exress_mate_Show", list);
        }
        //添加(删除)匹配信息
        public int AddMateCompany(int Express_Company_Id, string P_Id)
        {
            List<ExpressMatch> list = new List<ExpressMatch>();
            DataTable data = bll.GetExpressMatch1(Express_Company_Id);
            foreach (DataRow item in data.Rows)
            {
                ExpressMatch e = new ExpressMatch();
                e.ExpressMatchId = Convert.ToInt32(item["ExpressMatchId"]);
                e.Express_Company_Id = Convert.ToInt32(item["Express_Company_Id"]);
                e.P_Id = Convert.ToInt32(item["P_Id"]);
                list.Add(e);
            }
            string[] Eid = P_Id.Split(',');
            if (list.Count == 0)
            {
                if (P_Id == "")
                {
                    bll.DelExpressMatch(Express_Company_Id);
                }
                else
                {
                    for (int i = 0; i < Eid.Length; i++)
                    {
                        bll.AddExpressMatch(Express_Company_Id, Convert.ToInt32(Eid[i]));
                    }
                }
                return 1;
            }

            if (list.Count > 0)
            {
                if (P_Id == "")
                {
                    bll.DelExpressMatch(Express_Company_Id);
                }
                else
                {
                    bll.DelExpressMatch(Express_Company_Id);
                    for (int i = 0; i < Eid.Length; i++)
                    {
                        bll.AddExpressMatch(Express_Company_Id, Convert.ToInt32(Eid[i]));
                    }
                }
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
}
